<template>
  <div>
    <!-- 表单内容 -->
    <el-form ref="perForm" :rules="rules" :model="formData" label-width="100px">
      <el-form-item label="权限名称" prop="name">
        <el-input v-model="formData.name" />
      </el-form-item>
      <el-form-item label="权限标识" prop="code">
        <el-input v-model="formData.code" />
      </el-form-item>
      <el-form-item label="权限描述">
        <el-input v-model="formData.description" />
      </el-form-item>
      <el-form-item label="权限启用">
        <el-switch
          v-model="formData.enVisible"
          active-text="启用"
          active-value="1"
          inactive-text="不启用"
          inactive-value="0"
        />
      </el-form-item>
      <el-form-item>
        <div>
          <el-button @click="cancel">取消</el-button>
          <el-button type="primary" @click="submitHandle">确定</el-button>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { addPermission, updatePermission } from '@/api/permission'
export default {
  name: '',
  props: {
    showDialog: {
      type: Boolean
    },
    pid: {
      type: [String, Number],
      default: ''
    },
    type: {
      type: [String, Number],
      default: '1'
    },
    isEdit: {
      type: Boolean
    },
    row: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      formData: {
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        enVisible: '0', // 开启
        pid: '', // 添加到哪个节点下
        type: '' // 类型
      },
      rules: {
        name: [{ required: true, message: '请输入权限名称', trigger: 'blur' }],
        code: [{ required: true, message: '请选择权限编码', trigger: 'blur' }]
      }
    }
  },
  computed: {},
  watch: {
    isEdit: {
      handler(newVal) {
        if (newVal) {
          this.formData = JSON.parse(JSON.stringify(this.row))
        }
      },
      immediate: true
    }
  },
  created() {},
  methods: {
    // 对话框确认按钮
    submitHandle() {
      this.$refs.perForm.validate(async(validate) => {
        if (validate) {
          // alert("成功");

          // console.log(this.formData, 88);
          // await addPermission(this.formData)
          if (this.isEdit) {
            // 编辑
            await updatePermission(this.formData)
          } else {
            // 添加
            this.formData.pid = this.pid
            this.formData.type = this.type
            await addPermission(this.formData)
          }
          this.close()
        }
      })
    },
    close() {
      /* 初始化表单 */
      // this.formData = {
      //   name: "", // 名称
      //   code: "", // 权限标识
      //   description: "", // 描述
      //   enVisible: "0", // 开启
      //   pid: "", // 添加到哪个节点下
      //   type: "", // 类型
      // };
      this.$emit('success')
    },
    cancel() {
      this.$emit('update:showDialog', false)
    }
  }
}
</script>
<style lang="less" scoped></style>
